/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mscs.cashier.logic;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import mscs.data.IFileDataStore;
import mscs.data.IGoodsGiftProxy;
import mscs.data.IMemberProxy;
import mscs.data.IReturnRecordProxy;
import mscs.data.ISaleRecordProxy;
import mscs.data.ITotalDiscountProxy;
import mscs.data.pojo.Goods;
import mscs.data.pojo.GoodsGift;
import mscs.data.pojo.Member;
import mscs.data.pojo.ReturnRecord;
import mscs.data.pojo.SaleRecord;
import mscs.data.pojo.TotalDiscount;

/**
 *
 * @author Cake
 */
public class CalculateUtil {
    
    
    private IFileDataStore allSellings = new ISaleRecordProxy();
    private List<SaleRecord> allSaleRecord=(ArrayList<SaleRecord>)allSellings.getObjects();
    private IFileDataStore allReturnings = new IReturnRecordProxy();
    private List<ReturnRecord> allReturnRecord=(ArrayList<ReturnRecord>)allSellings.getObjects();
    
    private ReturnRecord newReturnRecord=new ReturnRecord();
    private SaleRecord newSaleRecord =new  SaleRecord();
    
    private IFileDataStore allGifts =new IGoodsGiftProxy();    
    private List<GoodsGift> allgiftList=(ArrayList<GoodsGift>)allGifts.getObjects();
    
    private IFileDataStore allMembers =new IMemberProxy();
    private List<Member> allMemberList=(ArrayList<Member>)allMembers.getObjects(); 
    
    private IFileDataStore allTotalDiscount=new ITotalDiscountProxy();
    private List<TotalDiscount> allTotalDiscountList=(ArrayList<TotalDiscount>)allTotalDiscount.getObjects(); 
    
   
     
    private SimpleDateFormat sdf;
    

    public double totalPrice=0.0;
    
   
   public void commitReturnInfo(){
       allReturnRecord.add(newReturnRecord);
        this.allReturnings.update(allReturnRecord);
        newSaleRecord =new SaleRecord();
   }
    
    public void commitInfo(Member newmember){
       // System.out.println("3  "+newSaleRecord.getId());
        for(Member member: allMemberList){  
            
             if(member.getId().equals(newmember.getId())){
                 
                 member.setRecord(newmember.getRecord());
                 member.setTotalPoints(newmember.getTotalPoints());                 
                
                 ArrayList<SaleRecord> newlist;
                 
                 if(member.getRecord()!=null){
                     newlist=member.getRecord();
      
                     newlist.add(newSaleRecord);
                 
                 member.setRecord(newlist);
                 }else{
                     newlist=new ArrayList<SaleRecord>();
                     newlist.add(newSaleRecord);                
                     member.setRecord(newlist);                
                 }
          
                 
                 allMembers.update(allMemberList);
             }
            
        }   
        
        allSaleRecord.add(newSaleRecord);
        
        this.allSellings.update(allSaleRecord);
        newSaleRecord =new SaleRecord();
    }
   
    
    
    public boolean memberLogin(Member newmember){
        
        for(Member member: allMemberList){
                if(member.getId().equals(newmember.getId())){
                    newmember.setName(member.getName());
                    newmember.setTotalPoints(member.getTotalPoints());
                    newmember.setBirthday(member.getBirthday());
                    return true;
                }
        }
        return false;
    } 
    
    //更新退货记录
    public void setReturnRecord(List<Goods> returningList, List<GoodsGift> returnGiftList, SaleRecord saleRecord){
         newReturnRecord.setSaleRecordID(saleRecord.getId());
         newReturnRecord.setGoodsList(returningList);
         newReturnRecord.setGiftsList(returnGiftList);
         newReturnRecord.setId(new SimpleDateFormat ("yyMMddHHmmss").format(new Date()));
         newReturnRecord.setTotalPrice(totalPrice);
         newReturnRecord.setDate(new Date());
     }
    
   //更新售货记录
    
    public void setSaleRecord(List<Goods> goodsList,List<GoodsGift> giftList){
        newSaleRecord.setId(new SimpleDateFormat ("yyMMddHHmmss").format(new Date()));//销售记录直接以时间作为编号
        newSaleRecord.setGiftsList(giftList);
        newSaleRecord.setDate(new Date());
        newSaleRecord.setGoodsList(goodsList);
        newSaleRecord.setTotalPrice(totalPrice);
        
        /*
        for(int i=0;i<allSaleRecord.size();i++){
            System.out.println(allSaleRecord.get(i).getGoodsList().get(0).getDesc());
             System.out.println(".....");
        }
        System.out.println(newSaleRecord.getId());
        System.out.println(newSaleRecord.getTotalPrice());
        System.out.println(new SimpleDateFormat ("yy-MM-dd").format(newSaleRecord.getDate()));
        */
      
        
    }
    
    
   
    public double getTotalPrice(){
        return totalPrice;
    }
    
    public double setTotalPrice(List<Double> priceList){
        for(double price:priceList){
            totalPrice+=price;
        }
        
        for(TotalDiscount discount:allTotalDiscountList){
            if(discount.getStartDate().before(new Date())&&discount.getEndDate().after(new Date())
                    &&totalPrice>=discount.getLowerLimit()&&totalPrice<=discount.getUpperLimit()){
                totalPrice*=discount.getDiscountRate();
            }
        }
        
        return totalPrice;
    }

   

     public List toStringArray(Goods goods) {
        Vector<String> list = new Vector<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");        
        return list;
    } 
     
      public Vector<Vector<String>> toDataTable(List list) {
    	try {
            Vector<Goods> seList = new Vector(list);
            Vector<Vector<String>> reList = new Vector<Vector<String>>();
            for (Goods goods : seList)
            {
                reList.add((Vector<String>)toStringArray(goods));
            }
            return reList;
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
    }

    
}
